package cn.e4j.modular.api.service;

import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.util.DateUtils;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.core.util.LocalDateUtils;
import cn.e4j.core.util.ToolUtil;
import cn.e4j.modular.api.mapper.Gwat155Mapper;
import cn.e4j.modular.api.util.CommonUtils;
import cn.e4j.modular.crm.entity.GwbmT;
import cn.e4j.modular.crm.mapper.GwbmMapper;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.time.temporal.TemporalAccessor;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * Created by wenchuan.li on 2018/6/14.
 */

@Service
public class Gwat155Service{

    @Autowired
    GwbmMapper gwbmMapper;

    @Autowired
    Gwat155Mapper gwat155Mapper;


    @Transactional
    public void gwat155Save(GwbmT gwbm_tPO) {
        gwbm_tPO.insert();
    }


    public List<Dto> Gwbm001check(Dto inDto) {
        return gwat155Mapper.getGwbm001(inDto);
    }

    @Transactional(readOnly = true)
    public List<Dto> getKH(Dto inDto) {
        List<Dto> list = InvokeUtil.invokeBeanMethod2("cn.e4j.modular.api.mapper.Gwat155Mapper.getKH",inDto);
        List<Dto> result = new ArrayList<>();
        for (Dto dto:list){
            String GWBA003 = dto.getString("GWBA003");
            String GWBA004 = dto.getString("GWBA004");
            if(ToolUtil.isNotEmpty(GWBA003) && ToolUtil.isNotEmpty(GWBA004)){
                boolean before = CommonUtils.isBefore(CommonUtils.addMinutes(GWBA003,-30), LocalDateTime.now());
                boolean after = CommonUtils.isAfter(CommonUtils.addMinutes(GWBA004,30),LocalDateTime.now());

                if ( after && before){
                    result.add(dto);
                }
            }

        }
        return result;
    }

}
